// pages/cost/product-list/index.js
import api from '../../../../services/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '',
    qd: '',
    qdName: '',
    productInfo: [],
    seletedProductList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    if (e.type) {
      let qdName= '零售';
      if (e.qd === 'ds') {
        qdName = '电商'
      } else if (e.qd === 'pdi') {
        qdName = 'PDI'
      }
      this.setData({
        type: e.type,
        qd: e.qd,
        qdName: qdName
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.queryProductList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  async queryProductList() {
    let channelType = 0;
    if (this.data.qd === 'ls') {
      channelType = 1
    } else if (this.data.qd === 'pdi') {
      channelType = 2;
    }
    console.log(api.quote)
    const res = await api.quote.queryProductList({
      channelType: channelType,
      productType: this.data.type === 'cy' ? 0 : 1
    });
    if (res.status == 200 && res.data && res.data.length > 0) {
      for (let i = 0; i < res.data.length; i ++) {
        let productListLeft = [];
        let productListRight = [];
        for (let j = 0; j < res.data[i].productList.length; j ++) {
          res.data[i].productList[j].selected = false;
          if (res.data[i].productList[j].windowType === 0 || res.data[i].productList[j].windowType === 3) {
            if (res.data[i].productList[j].windowType === 0) {
              res.data[i].productList[j].productName = res.data[i].productList[j].productName + ' (前挡)';
            } else {
              res.data[i].productList[j].productName = res.data[i].productList[j].productName + ' (天窗)';
            }
            productListLeft.push(res.data[i].productList[j]);
          } else {
            res.data[i].productList[j].windowType = 1;
            res.data[i].productList[j].productName = res.data[i].productList[j].productName + ' (侧后挡)';
            productListRight.push(res.data[i].productList[j]);
          }
        }
        res.data[i].productListLeft = productListLeft;
        res.data[i].productListRight = productListRight;
      }
      this.setData({
        productInfo: res.data
      })
    }
  },
  confirmQuery: function () {
    let params = [];
    for (let i = 0; i < this.data.productInfo.length; i ++) {
      for (let j = 0; j < this.data.productInfo[i].productListLeft.length; j ++) {
        if (this.data.productInfo[i].productListLeft[j].selected) {
          params.push({
            seriesName: this.data.productInfo[i].seriesName,
            productId: this.data.productInfo[i].productListLeft[j].productId,
            productName: this.data.productInfo[i].productListLeft[j].productName
          })
        }
      }
      for (let j = 0; j < this.data.productInfo[i].productListRight.length; j ++) {
        if (this.data.productInfo[i].productListRight[j].selected) {
          params.push({
            seriesName: this.data.productInfo[i].seriesName,
            productId: this.data.productInfo[i].productListRight[j].productId,
            productName: this.data.productInfo[i].productListRight[j].productName
          })
        }
      }
    }
    if (params.length === 0) {
      wx.showToast({
        title: "请选择产品",
        icon: "none",
        duration: 1500
      })
      return;
    }
    let paramsStr = JSON.stringify(params);
    wx.navigateTo({
      url: `/pages/index/cost/result-cm/index?type=${this.data.type}&qd=${this.data.qd}&productList=${paramsStr}`,
    });
  },
  selectProduct: function (e) {
    const productId = e.currentTarget.dataset.productid;
    const windowType = e.currentTarget.dataset.windowtype;
    const selectType = e.currentTarget.dataset.selecttype;
    for (let i = 0; i < this.data.productInfo.length; i ++) {
      for (let j = 0; j < this.data.productInfo[i].productListLeft.length; j ++) {
        if (this.data.productInfo[i].productListLeft[j].windowType === windowType) {
          this.data.productInfo[i].productListLeft[j].selected = false;
        }
      }
      for (let j = 0; j < this.data.productInfo[i].productListRight.length; j ++) {
        if (this.data.productInfo[i].productListRight[j].windowType === windowType) {
          this.data.productInfo[i].productListRight[j].selected = false;
        }
      }
    }
    if (!selectType) {
      for (let i = 0; i < this.data.productInfo.length; i ++) {
        for (let j = 0; j < this.data.productInfo[i].productListLeft.length; j ++) {
          if (this.data.productInfo[i].productListLeft[j].productId === productId) {
            this.data.productInfo[i].productListLeft[j].selected = true;
          }
        }
        for (let j = 0; j < this.data.productInfo[i].productListRight.length; j ++) {
          if (this.data.productInfo[i].productListRight[j].productId === productId) {
            this.data.productInfo[i].productListRight[j].selected = true;
          }
        }
      }
    }
    this.setData({
      productInfo: this.data.productInfo
    })
  },
})